const router = require('express').Router();
const multer = require('multer');
const path = require('path');
// 自定义上传路径和文件名
 const storage = multer.diskStorage({
    // 存储文件目录
    destination: function (req, file, cb) {
      cb(null, './public/uploads')
    },
    // 自定义存储文件名
    filename: function (req, file, cb) {
      cb(null, file.fieldname + '-' + Date.now()+path.extname(file.originalname))
    }
})

const upload = multer({ storage: storage });

router.get('/upload', (req,res) => {
    res.render('upload')
})
router.post('/upload', upload.single('file'), (req,res) => {
    // req.file 上传文件基础信息
    res.send({
        code: 200,
        msg: '上传成功',
        data: {
            url: '/static/uploads/'+ req.file.filename
        }
    })
})
module.exports = router